package com.twt.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.twt.common.utils.MD5Util;
import com.twt.model.system.entity.SysUser;
import com.twt.common.pojo.ResultCodeEnum;
import com.twt.common.exceptions.AppException;
import com.twt.sys.service.LoginService;
import com.twt.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

@Service
@Transactional
public class LoginServiceImpl implements LoginService {
    @Autowired
    private SysUserService sysUserService;

    @Override
    public SysUser login(SysUser sysUser) {
        Assert.notNull(sysUser,"sysUser is null");
        Assert.hasText(sysUser.getAccount(),"登录账号不能为空");
        Assert.hasText(sysUser.getPassword(),"登录密码不能为空");

        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("account",sysUser.getAccount());
        SysUser user = sysUserService.getOne(queryWrapper);
        String encode = sysUser.getPassword();//MD5Util.encode(sysUser.getPassword());
        if (user==null){
            throw new AppException(ResultCodeEnum.ACC_PWD_WRONG);
        }
        if (!encode.equals(user.getPassword())){
            throw new AppException(ResultCodeEnum.ACC_PWD_WRONG);
        }
        return user;
    }
}
